/**
 * @author 杨执信
 * @controller 获取数据库文章数据返回给前端
*/
/* eslint-disable*/
'use strict'

const Controller = require('egg').Controller

class HomeController extends Controller {
  async index() {
    // let result = await this.app.mysql.get("article", {})
    this.ctx.body = 'hello'
  }

  // 获取首页文章列表
  async getIndexList() {
    let sql = 'SELECT article.id as id ,' +
      'article.title as title ,' +
      'article.introduce as introduce ,' +
      'article.addTime as addTime ,' +
      'article.view_count as view_count ,' +
      'type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id'
    const result = await this.app.mysql.query(sql)
    this.ctx.body = { data: result }
  }

  // 根据id获取详情页文章
  async getDetailArticleById() {
    let id = this.ctx.params.id
    // console.log(id, '+++++++++++');
    let sql = 'SELECT article.id as id ,' +
      'article.title as title ,' +
      'article.introduce as introduce ,' +
      'article.article_content as article_content ,' +
      'article.addTime as addTime ,' +
      'article.view_count as view_count ,' +
      'type.typeName as typeName, ' +
      'type.id as typeId ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'WHERE article.id=' + id
    const result = await this.app.mysql.query(sql)
    this.ctx.body = { data: result }
  }

  // 根据类型id获取日常学习文章列表
  async getStudyListById() {
    let sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      'article.addTime as addTime ,' +
      'article.view_count as view_count ,' +
      'type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'WHERE type_id=' + 0
    const result = await this.app.mysql.query(sql)
    this.ctx.body = { data: result }
  }

  // 根据类型id获取技巧分享页文章列表
  async getShareListById() {
    let sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      'article.addTime as addTime ,' +
      'article.view_count as view_count ,' +
      'type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'WHERE type_id=' + 1
    const result = await this.app.mysql.query(sql)
    this.ctx.body = { data: result }
  }
}

module.exports = HomeController
